﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Algorithm.Phan1.BaiToanLietKe._2.PhuongPhapSinh
{
    class LietKeTapConK
    {

        public LietKeTapConK()
        {
            
        }

        /// <summary>
        /// Liet ke cac tap con k phan tu cua day tu nhien 1..n.
        /// Gioi han tren : n-k+i+1
        /// </summary>
        /// <param name="n">So n phan tu cua day</param>
        /// <param name="k">Tap con k phan tu</param>
        public LietKeTapConK(int n,int k)
        {
            if (n<k)
            {
                Console.WriteLine("Error");
                return;
            }

            var x = new int[100];
            var result = new StringBuilder();
            int h = 1,l=k-1;

            for (int i = 0; i < n; i++)
            {
                x[i] = i + 1;
            }

            while (x[0]<=n-k+1) {
                for (int i = 0; i < k; i++)
                {
                    result.Append(x[i].ToString());
                }
                result.AppendLine();
                l = k - 1;
                while (l>-1 && x[l] == l+n-k+1) {
                    l--;
                }

                if (l>-1)
                {
                    x[l]++;
                    for (int i = l + 1; i < k; i++)
                    {
                        x[i] = x[i - 1] + 1;
                    }
                }
                
            }

            Console.WriteLine(result);
            
        }



    }
}
